import com.didano.tcpserver.tcp.client.QpsMockData;
import com.didano.tcpserver.tcp.common.EchoConstant;
import com.didano.tcpserver.tcp.dto.RopeSkipUpDTO;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.IntStream;

/**
 * https://mp.weixin.qq.com/s/VdyXDBevE48Wtr95ug_aKw
 *
 * 代码比较简单，直接使用BIO串行创建30000个连接。
 *
 * go语言服务端-测试
 */
@Slf4j
public class GoServerV3_MockRope {
    static String ip = "192.168.110.94";
    private static int port = 8888;


    public static void main(String[] args) throws IOException {
        System.out.println(Arrays.stream(args).toArray());

        int clientSize = 500;
        if (args.length > 0){
            clientSize = Integer.parseInt(args[0]);
        }
        performanceTest();
    }

    @SneakyThrows
    private static void performanceTest() throws IOException {
        LinkedList<SocketChannel> clients = new LinkedList<>();
        InetSocketAddress serverAddr = new InetSocketAddress(ip, port);
        IntStream.range(20000, 50000).forEach(i -> {
//        IntStream.range(20000, 20100).forEach(i -> {
            try {
                SocketChannel client = SocketChannel.open();
                client.bind(new InetSocketAddress(ip, i));
                client.connect(serverAddr);
                String timeNow = DateTime.now().toString();
                String json = "{\"name\":\"" + i + "\",\"content\":\"full\",\"12\":0,\"11\":60,\"8\":66,\"6\":3,\"1\":1,\"4\":101,\"9\":0}" + EchoConstant.SEPARATOR;
                client.write(ByteBuffer.wrap(json.getBytes(StandardCharsets.UTF_8)));
                System.out.println("client:" + i + " connected");
                clients.add(client);

            } catch (IOException e) {
                System.out.println("IOException" + i);
                e.printStackTrace();
            }
        });
        System.out.println("clients.size: " + clients.size());
        //阻塞主线程
        System.in.read();
    }
}
